
************** Spatial approach, 6: Regressions for Step-Function ******************
* spatial transformation completed previously in spatial_1_* - spatial_5_*

************************************
* Contents
		* Grid based: H horizontal, R vertical, W (west) and O (east) indicate direction 
		* 4950 cells based on k_bins of width 3 KwH 
		* Outcomes: sales and count, each in level, log+1, and ihs transformation
		* standard errors clustered by cell   
************************************

************************************
* Variables
		* cm: country-month items (second level identifier)
		* cell: 1-50 are bunchers at window = 2. (primary identifier)
		* a_bin/e_bin: individual attributes, unstandardized 
		* csd  / ccd: sales / product count in cell. 
		

 clear
 capture log close
 set more off
 
capture mkdir Data
global store "R:\WSV2\TBu_AKe\Spatial_NEW\Data"
 
cd "R:\WSV2\TBu_AKe\Spatial_NEW"

 *clear
 set more 1
 capture log close

 log using spatial_6_regs_grid_2703, replace   
 

			***************************
			* run regression: OLS, fe *
			***************************		
			
use spatial_regs_grid_monthly, replace  
			
			*** Outcome #1 : sales 
			
*preserve
spmatrix dir 
keep if year == 2014 // no changes. otherwise matrices are wrong in previous. 	
	
	
			***************************
			***** Single Matrix *****
			***************************	
			
***************** OUTCOME SALES LEVEL ****************************


			**********************************************
			***** #1: country and cell fixed-effects *****
			**********************************************
	

** log and ihs transformations to the untransformed data **

		*** outcomes and polar cases 
foreach varx of varlist csd L3_sd R_L3_sd H_L3_sd 		ccd L3_cd    R_L3_cd H_L3_cd {

   gen     log_`varx'=log(1+`varx')
   gen     ihs_`varx'=`varx'+(`varx'^2+1)^0.5
   replace ihs_`varx'=log(ihs_`varx')
 }
 
		*** spatial variables *** 
forvalues q = 5(5)40 { 
	foreach varx of varlist 	W`q'_L3_sd O`q'_L3_sd 	W`q'_L3_cd O`q'_L3_cd {

   gen     log_`varx'=log(1+`varx')
   gen     ihs_`varx'=`varx'+(`varx'^2+1)^0.5
   replace ihs_`varx'=log(ihs_`varx')
	}
}

** Results for untransformed data **

             *** Polar cases **    

reghdfe  csd    L3_sd 	R_L3_sd					if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell)  
estimates store fe2_R

reghdfe  csd    L3_sd 	H_L3_sd					if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell)  
estimates store fe2_H

			 *** Northwest quadrant
			 
forvalues q = 5(5)40 {
	reghdfe  csd    L3_sd 	W`q'_L3_sd					if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) 
	estimates store fe2_W`q'
}

			 *** Northeast quadrant
			 
forvalues q = 5(5)40 {
	reghdfe  csd    L3_sd 	O`q'_L3_sd					if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) 
	estimates store fe2_O`q'
}			 



    ** Results for log transformed data **

             *** Polar cases **    

reghdfe  log_csd    log_L3_sd 	log_R_L3_sd					if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell)  
estimates store log_fe2_R

reghdfe  log_csd    log_L3_sd 	log_H_L3_sd					if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell)  
estimates store log_fe2_H

			 *** Northwest quadrant
			 
forvalues q = 5(5)40 {
	reghdfe  log_csd    log_L3_sd 	log_W`q'_L3_sd					if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) 
	estimates store log_fe2_W`q'
}

			 *** Northeast quadrant
			 
forvalues q = 5(5)40 {
	reghdfe  log_csd    log_L3_sd 	log_O`q'_L3_sd					if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) 
	estimates store log_fe2_O`q'
}


    ** Results for ihs transformed data **


             *** Polar cases **    

reghdfe  ihs_csd    ihs_L3_sd 	ihs_R_L3_sd					if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell)  
estimates store ihs_fe2_R

reghdfe  ihs_csd    ihs_L3_sd 	ihs_H_L3_sd					if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell)  
estimates store ihs_fe2_H

			 *** Northwest quadrant
			 
forvalues q = 5(5)40 {
	reghdfe  ihs_csd    ihs_L3_sd 	ihs_W`q'_L3_sd					if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) 
	estimates store ihs_fe2_W`q'
}

			 *** Northeast quadrant
			 
forvalues q = 5(5)40 {
	reghdfe  ihs_csd    ihs_L3_sd 	ihs_O`q'_L3_sd					if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) 
	estimates store ihs_fe2_O`q'
}


***************** OUTCOME Count ****************************
	

** Results for untransformed data **

             *** Polar cases **    

reghdfe  ccd    L3_cd 	R_L3_cd					if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell)  
estimates store feC_R

reghdfe  ccd    L3_cd 	H_L3_cd					if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell)  
estimates store feC_H

			 *** Northwest quadrant
			 
forvalues q = 5(5)40 {
	reghdfe  ccd    L3_cd 	W`q'_L3_cd					if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) 
	estimates store feC_W`q'
}

			 *** Northeast quadrant
			 
forvalues q = 5(5)40 {
	reghdfe  ccd    L3_cd 	O`q'_L3_cd					if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) 
	estimates store feC_O`q'
}			 



    ** Results for log transformed data **

             *** Polar cases **    

reghdfe  log_ccd    log_L3_cd 	log_R_L3_cd					if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell)  
estimates store log_feC_R

reghdfe  log_ccd    log_L3_cd 	log_H_L3_cd					if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell)  
estimates store log_feC_H

			 *** Northwest quadrant
			 
forvalues q = 5(5)40 {
	reghdfe  log_ccd    log_L3_cd 	log_W`q'_L3_cd					if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) 
	estimates store log_feC_W`q'
}

			 *** Northeast quadrant
			 
forvalues q = 5(5)40 {
	reghdfe  log_ccd    log_L3_cd 	log_O`q'_L3_cd					if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) 
	estimates store log_feC_O`q'
}


    ** Results for ihs transformed data **


             *** Polar cases **    

reghdfe  ihs_ccd    ihs_L3_cd 	ihs_R_L3_cd					if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell)  
estimates store ihs_feC_R

reghdfe  ihs_ccd    ihs_L3_cd 	ihs_H_L3_cd					if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell)  
estimates store ihs_feC_H

			 *** Northwest quadrant
			 
forvalues q = 5(5)40 {
	reghdfe  ihs_ccd    ihs_L3_cd 	ihs_W`q'_L3_cd					if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) 
	estimates store ihs_feC_W`q'
}

			 *** Northeast quadrant
			 
forvalues q = 5(5)40 {
	reghdfe  ihs_ccd    ihs_L3_cd 	ihs_O`q'_L3_cd					if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) 
	estimates store ihs_feC_O`q'
}

		********************
		** report results **
		********************
		
		******* reghdfe LEVEL w/ country and cell fixed-effects ******
					
estimates table fe2_R fe2_H fe2_W*, b(%9.4f) stats(N r2 r2_a F) star(0.10 .05 .01)

estimates table fe2_R fe2_H fe2_O*, b(%9.4f) stats(N r2 r2_a F) star(0.10 .05 .01)


		******* reghdfe LOG w/ country and cell fixed-effects ******
					
estimates table  log_fe2_R log_fe2_H log_fe2_W*, b(%9.4f) stats(N r2 r2_a F) star(0.10 .05 .01)
estimates table  log_fe2_R log_fe2_H log_fe2_O*, b(%9.4f) stats(N r2 r2_a F) star(0.10 .05 .01)


		******* reghdfe IHS w/ country and cell fixed-effects ******

estimates table  ihs_fe2_R ihs_fe2_H ihs_fe2_W*, b(%9.4f) stats(N r2 r2_a F) star(0.10 .05 .01)
estimates table  ihs_fe2_R ihs_fe2_H ihs_fe2_O*, b(%9.4f) stats(N r2 r2_a F) star(0.10 .05 .01)


		******* reghdfe for product count as outcome *************
/* 

		******* reghdfe LEVEL w/ country and cell fixed-effects ******
					
estimates table feC_R feC_H feC_W*, b(%9.4f) stats(N r2 r2_a F) star(0.10 .05 .01)

estimates table feC_R feC_H feC_O*, b(%9.4f) stats(N r2 r2_a F) star(0.10 .05 .01)


		******* reghdfe LOG w/ country and cell fixed-effects ******
					
estimates table  log_feC_R log_feC_H log_feC_W*, b(%9.4f) stats(N r2 r2_a F) star(0.10 .05 .01)
estimates table  log_feC_R log_feC_H log_feC_O*, b(%9.4f) stats(N r2 r2_a F) star(0.10 .05 .01)


		******* reghdfe IHS w/ country and cell fixed-effects ******

estimates table  ihs_feC_R ihs_feC_H ihs_feC_W*, b(%9.4f) stats(N r2 r2_a F) star(0.10 .05 .01)
estimates table  ihs_feC_R ihs_feC_H ihs_feC_O*, b(%9.4f) stats(N r2 r2_a F) star(0.10 .05 .01)

*/ 

log close 

clear 

exit 
